namespace SimpleWebServices.Web.Mvc
{
    using System;
    using System.Globalization;
    using System.Threading;
    using System.Web;
    using System.Web.Mvc;
    using System.Web.Routing;
    using Moq;
    using SimpleWebServices;
    using SimpleWebServices.Data;
    using SimpleWebServices.Models;
    using SimpleWebServices.Web.Routing;
    using Xunit;

    public sealed class TenantHtmlResultControllerFacts
    {
        [Fact]
        public void typedef()
        {
            Assert.True(new ObjectExpectations<TenantHtmlResultController>()
                .DerivesFrom<TenantController>()
                .IsConcreteClass()
                .IsSealed()
                .HasDefaultConstructor()
                .IsDecoratedWith<InternalServerErrorAttribute>()
                .Allow("GET, HEAD, OPTIONS")
                .IsDecoratedWith<CacheControlAttribute>()
                .IsDecoratedWith<ContentLanguageAttribute>()
                .IsDecoratedWith<ContentLocationAttribute>()
                .IsDecoratedWith<ContentMD5Attribute>()
                .Result);
        }

        [Fact]
        public void ctor()
        {
            Assert.NotNull(new TenantHtmlResultController());
        }

        [Fact]
        public void op_Register_RouteCollectionNull()
        {
            Assert.Throws<ArgumentNullException>(() => (new TenantHtmlResultController() as IRegisterRoutes).Register(null));
        }

        [Fact]
        public void op_Register_RouteCollection()
        {
            RouteCollection routes = new RouteCollection();
            (new TenantHtmlResultController() as IRegisterRoutes).Register(routes);

            Assert.IsType<HtmlResultRoute>(routes[0]);
        }

        [Fact]
        public void op_HtmlRepresentation()
        {
            FakeRepository.Clear();

            CultureInfo culture = Thread.CurrentThread.CurrentUICulture;

            try
            {
                Token token = Token.New();

                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml =
                        "<tenant>" +
                            "<master-pages>" +
                                "<value name='html'>HtmlRepresentation.Default</value>" +
                            "</master-pages>" +
                        "</tenant>"
                });
                FakeRepository.Register<FormSubmission>(new
                {
                    token = token,
                    urn = "urn://result/" + Token.New(),
                    xml = "<result><div id='result' /></result>"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/result.en.html").Verifiable();
                mock.SetupGet<string>(x => x.Request.RawUrl).Returns("/sample/result.en.html?" + token).Verifiable();
                HttpContextFactory.Mock = mock.Object;

                Assert.IsType<ViewResult>(new TenantHtmlResultController().HtmlRepresentation(new CultureInfo("en")));

                Assert.Equal<CultureInfo>(new CultureInfo("en"), Thread.CurrentThread.CurrentUICulture);

                mock.VerifyAll();
            }
            finally
            {
                Thread.CurrentThread.CurrentUICulture = culture;

                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }

        [Fact]
        public void op_XhtmlRepresentation()
        {
            FakeRepository.Clear();

            CultureInfo culture = Thread.CurrentThread.CurrentUICulture;

            try
            {
                Token token = Token.New();

                FakeRepository.Register<Tenant>(new
                {
                    urn = "urn://example.com/tenant/sample",
                    xml =
                        "<tenant>" +
                            "<master-pages>" +
                                "<value name='xhtml'>XhtmlRepresentation.Default</value>" +
                            "</master-pages>" +
                        "</tenant>"
                });
                FakeRepository.Register<FormSubmission>(new
                {
                    token = token,
                    urn = "urn://result/" + Token.New(),
                    xml = "<result><div id='result' /></result>"
                });

                var mock = new Mock<HttpContextBase>();
                mock.SetupGet<string>(x => x.Request.Path).Returns("/sample/result.en.xhtml").Verifiable();
                mock.SetupGet<string>(x => x.Request.RawUrl).Returns("/sample/result.en.xhtml?" + token).Verifiable();
                HttpContextFactory.Mock = mock.Object;

                Assert.IsType<ViewResult>(new TenantHtmlResultController().XhtmlRepresentation(new CultureInfo("en")));

                Assert.Equal<CultureInfo>(new CultureInfo("en"), Thread.CurrentThread.CurrentUICulture);

                mock.VerifyAll();
            }
            finally
            {
                Thread.CurrentThread.CurrentUICulture = culture;

                FakeRepository.Reset();
                HttpContextFactory.Reset();
            }
        }
    }
}